﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace PowerCard
{
    public class SocketSend
    {

        private bool getTempOrNot(byte[] byteMessage)
        {
            int count = 0;
            for (int i = 0; i < 50; i++)
            {
                if (byteMessage[i].ToString("X2") != "00")
                {
                    count += 1;
                    break;
                }
            }
            if (count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public int Send_SMS_Point(Socket _socket, DataRow rw)
        {
            int a = 0;
            string result = "";
            try
            {
                if (rw["MO_Operation"].ToString().Trim() == "E500")
                {
                    int temp = 1000;
                    if (int.Parse(rw["MO_Power_Count"].ToString().Trim()) != 9999)
                        temp += int.Parse(rw["MO_Power_Count"].ToString().Trim());
                    else
                        temp = int.Parse(rw["MO_Power_Count"].ToString().Trim());

                    PowerCard.Power.Write("E500" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());

                    byte[] bytes = powerAdd("E500", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), rw["MO_Power_Consumption"].ToString().Trim(), temp);


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发E500_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收E500_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }
                }
                else if (rw["MO_Operation"].ToString().Trim() == "E502")
                {
                    PowerCard.Power.Write("E502" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = CopyRemainPower("E502", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发E502_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[1024];

                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收E502_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "81")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                    if (rw["MO_MC_No"].ToString().Trim().Substring(0, 1) == "1")
                    {
                        insertDb(byteMessage);
                    }

                }

                else if (rw["MO_Operation"].ToString().Trim() == "C03C")
                {
                    PowerCard.Power.Write("C03C" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = CartPower("C03C", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发C03C_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收C03C_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                }
                else if (rw["MO_Operation"].ToString().Trim() == "C03D")
                {
                    PowerCard.Power.Write("C03D" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = UpPower("C03D", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发C03D_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收C03D_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                }

                else if (rw["MO_Operation"].ToString().Trim() == "C03E")
                {
                    PowerCard.Power.Write("C03E" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = noyufu("C03E", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发C03E_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收C03E_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                }

                else if (rw["MO_Operation"].ToString().Trim() == "C03F")
                {
                    PowerCard.Power.Write("C03F" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = noyufu("C03F", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发C03F_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收C03F_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                }

                else if (rw["MO_Operation"].ToString().Trim() == "E870")
                {
                    PowerCard.Power.Write("E870" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = powerfuhe("E870", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), rw["MO_Power_Consumption"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发E870_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收E870_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "81")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                    if (rw["MO_MC_No"].ToString().Trim().Substring(0, 1) == "1")
                    {
                        insertDb(byteMessage);
                    }

                }

                else if (rw["MO_Operation"].ToString().Trim() == "C625")
                {
                    PowerCard.Power.Write("C625" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = powerfuhe("C625", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), rw["MO_Power_Consumption"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发C625_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收C625_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }
                }
                else if (rw["MO_Operation"].ToString().Trim() == "E874")
                {
                    PowerCard.Power.Write("E874" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = powerqita("E874", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), "A0");


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发E874_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收E874_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }
                }

                else if (rw["MO_Operation"].ToString().Trim() == "E876")
                {
                    PowerCard.Power.Write("E876" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    byte[] bytes = powerqita("E876", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), "A0");


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发E876_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收E876_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }
                }
                else if (rw["MO_Operation"].ToString().Trim() == "E503")
                {
                    PowerCard.Power.Write("E503" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());
                    int temppower11 = (int)decimal.Parse(rw["MO_Power_Consumption"].ToString().Trim());
                    byte[] bytes = powerqita("E503", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), temppower11.ToString());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发E503_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收E503_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "84")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }
                }

                else if (rw["MO_Operation"].ToString().Trim() == "9010")
                {

                    PowerCard.Power.Write("9010" + rw["MO_MC_No"].ToString() + "_" + rw["MO_MB_No"].ToString().Trim());

                    byte[] bytes = CopyRemainPower("9010", rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());


                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------下发9010_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            PowerCard.Power.Write(bytes[i].ToString("X2"));
                        }
                    }

                    _socket.Send(bytes);

                    byte[] byteMessage = new byte[100];
                    _socket.Receive(byteMessage);

                    if (rw["MO_MB_No"].ToString() == System.Configuration.ConfigurationSettings.AppSettings["Log_MB_No"].ToString())
                    {
                        PowerCard.Power.Write("-------------收9010_" + rw["MO_Id"].ToString() + "---------------");
                        for (int i = 0; i < byteMessage.Length; i++)
                        {
                            PowerCard.Power.Write(byteMessage[i].ToString("X2"));
                        }
                    }

                    if (!getTempOrNot(byteMessage))
                    {
                        a = -1;
                    }
                    else
                    {
                        if (byteMessage[12].ToString("X2") == "81")
                        {
                            result = "下发成功！";
                        }
                        else
                        {
                            result = "下发失败！【" + byteMessage[13].ToString("X2") + byteMessage[14].ToString("X2") + byteMessage[15].ToString("X2") + "】";
                        }
                    }

                    if (rw["MO_MC_No"].ToString().Trim().Substring(0, 1) == "1")
                    {
                        insertDb(byteMessage);

                    }

                }


                if (a != -1)
                {
                    a = 1;
                    string sql1 = "UPDATE Meter_Operation SET MO_State=1,MO_Recive_Date='" + DateTime.Now.ToString() + "',MO_Result='" + result + "' WHERE MO_Id=" + rw["MO_Id"].ToString();
                    DBClass.ExeNonQuery(sql1);
                }
                else
                {
                    string sql1 = "UPDATE Meter_Operation SET MO_State=MO_State-10,MO_Recive_Date='" + DateTime.Now.ToString() + "'WHERE MO_Id=" + rw["MO_Id"].ToString();
                    DBClass.ExeNonQuery(sql1);
                }
                //DBClass.ExeNonQuery1(sql1);
                return a;
            }
            catch (Exception ex)
            {
                PowerCard.Power.Write("-------------------异常_" + rw["MO_MC_No"].ToString() + "," + rw["MO_Id"].ToString() + "--------------");
                PowerCard.Power.Write(ex.StackTrace);
                PowerCard.Power.Write(ex.Message.ToString());

                string sql1 = "UPDATE Meter_Operation SET MO_State=MO_State-10,MO_Recive_Date='" + DateTime.Now.ToString() + "'WHERE MO_Id=" + rw["MO_Id"].ToString();
                DBClass.ExeNonQuery(sql1);

                return -1;
            }

        }


        /// <summary>
        /// 调试工具
        /// </summary>
        /// <param name="_socket"></param>
        /// <param name="rw"></param>
        public void Send_Copy_Point(Socket _socket, DataRow rw)
        {
            byte[] bytes;
            Power po;
            string[] strbyte;
            string strReport;
            int i;
            if (rw["MO_Type"].ToString().Trim() == "0")
            {
                bytes = this.CopyRemainPower(rw["MO_Operation"].ToString(), rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());
                po = new Power();
                strbyte = po.Get16Byte(bytes);
                strReport = "";
                for (i = 0; i < strbyte.Length; i++)
                {
                    strReport = strReport + strbyte[i];
                }
                DBClass.ExeNonQuery("UPDATE Meter_Operation_Debug SET MO_Report='" + strReport + "' WHERE MO_Id=" + rw["MO_Id"].ToString());
                _socket.Send(bytes);
            }
            else if (rw["MO_Type"].ToString().Trim() == "1")
            {
                if (rw["MO_Lenth"].ToString().Trim() == "1")
                {
                    bytes = this.powerqita(rw["MO_Operation"].ToString(), rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), rw["MO_Power_Consumption"].ToString());
                    po = new Power();
                    strbyte = po.Get16Byte(bytes);
                    strReport = "";
                    for (i = 0; i < strbyte.Length; i++)
                    {
                        strReport = strReport + strbyte[i];
                    }
                    _socket.Send(bytes);
                    DBClass.ExeNonQuery("UPDATE Meter_Operation_Debug SET MO_Report='" + strReport + "' WHERE MO_Id=" + rw["MO_Id"].ToString());
                }
                else if (rw["MO_Lenth"].ToString().Trim() == "2")
                {
                    bytes = this.powerfuhe(rw["MO_Operation"].ToString(), rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim(), rw["MO_Power_Consumption"].ToString());
                    po = new Power();
                    strbyte = po.Get16Byte(bytes);
                    strReport = "";
                    for (i = 0; i < strbyte.Length; i++)
                    {
                        strReport = strReport + strbyte[i];
                    }
                    _socket.Send(bytes);
                    DBClass.ExeNonQuery("UPDATE Meter_Operation_Debug SET MO_Report='" + strReport + "' WHERE MO_Id=" + rw["MO_Id"].ToString());
                }
                else if (rw["MO_Lenth"].ToString().Trim() == "0")
                {
                    bytes = this.CartPower(rw["MO_Operation"].ToString(), rw["MO_MB_No"].ToString().Trim(), rw["MO_Pwd"].ToString().Trim());
                    strbyte = new Power().Get16Byte(bytes);
                    strReport = "";
                    for (i = 0; i < strbyte.Length; i++)
                    {
                        strReport = strReport + strbyte[i];
                    }
                    _socket.Send(bytes);
                    DBClass.ExeNonQuery("UPDATE Meter_Operation_Debug SET MO_Report='" + strReport + "' WHERE MO_Id=" + rw["MO_Id"].ToString());
                }
            }
            //DBClass.ExeNonQuery("UPDATE Meter_Operation_Debug SET MO_State=1 WHERE MO_Id=" + rw["MO_Id"].ToString());

            //_socket.Close();
        }


        //超水表
        public int Send_Copy_Water(Socket _socket, DataRow rw)
        {
            try
            {
                byte[] bytes;
                Power po;
                string[] strbyte;
                string strReport;
                int i;

                bytes = this.CopyRemainPower(rw["CW_Cmd"].ToString(), rw["CW_Mater_No"].ToString().Trim(), "93122600");

                _socket.Send(bytes);

                DBClass.ExeNonQuery("UPDATE Copy_Water SET CW_State=1 WHERE CW_Id=" + rw["CW_Id"].ToString());
                //PowerCard.Power.WriteWater("----水表------" + rw["CW_Id"].ToString());
                if (rw["CW_Meter_No"].ToString().Trim().Substring(0, 1) != "1")
                {

                    // _socket.Close();
                }
                else
                {
                    byte[] byteMessage;
                    byteMessage = new byte[1024];
                    Thread.Sleep(500);
                    //PowerCard.Power.Write("----Wait shuju------");
                    // PowerCard.Power.WriteWater("----Wait shuju水表------");
                    _socket.Receive(byteMessage);
                    //PowerCard.Power.WriteWater("----byteMessage------" + byteMessage[14].ToString() + "   ===" + byteMessage[13].ToString());
                    insertDb(byteMessage);

                    //_socket.Close();
                }
                return 1;
            }
            catch (Exception ex)
            {
                PowerCard.Power.Write("-------------------异常004_--------------");
                PowerCard.Power.Write(ex.StackTrace);
                PowerCard.Power.Write(ex.Message.ToString());

                return -1;
            }

        }

        private void insertDb(byte[] byteMessage)
        {

            if (byteMessage[13] == (byte)(Convert.ToInt16("E5", 16) + 0x33) && byteMessage[12] == (byte)(Convert.ToInt16("02", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";
                string PoNo0 = Convert.ToString(byteMessage[8], 16);
                string PoNo1 = Convert.ToString(byteMessage[7], 16);
                string PoNo2 = Convert.ToString(byteMessage[6], 16);
                string PoNo3 = Convert.ToString(byteMessage[5], 16);
                string PoNo4 = Convert.ToString(byteMessage[4], 16);
                string PoNo5 = Convert.ToString(byteMessage[3], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }

                int ff = ((byteMessage[16] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));
                ff = ((byteMessage[15] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[14] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;


                if (decimal.Parse(Power) > 9000.00M)
                {
                    decimal temppower = decimal.Parse(Power) - 9999.99M;
                    Power = temppower.ToString();
                }
                string sql = "SELECT TOP 1 * FROM Meter_Operation WHERE MO_Operation='E502' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                DataTable tb = DBClass.GetDataTable1(sql);
                if (tb.Rows.Count > 0)
                {
                    string Cell_Id = "0";
                    string Rom_Id = "0";
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                        Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                        Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();
                    sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Remain_Power_Consumption,CPC_Copy_Day) VALUES(";
                    sql += Rom_Id + "," + Cell_Id + "," + Power + ",'" + DateTime.Now.ToString() + "')";
                    DBClass.ExecuteNonQuery(sql);
                }

            }

            if (byteMessage[17] == (byte)(Convert.ToInt16("E5", 16) + 0x33) && byteMessage[16] == (byte)(Convert.ToInt16("02", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";
                string PoNo0 = Convert.ToString(byteMessage[12], 16);
                string PoNo1 = Convert.ToString(byteMessage[11], 16);
                string PoNo2 = Convert.ToString(byteMessage[10], 16);
                string PoNo3 = Convert.ToString(byteMessage[9], 16);
                string PoNo4 = Convert.ToString(byteMessage[8], 16);
                string PoNo5 = Convert.ToString(byteMessage[7], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }


                int ff = ((byteMessage[20] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));
                ff = ((byteMessage[19] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[18] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;



                if (decimal.Parse(Power) > 9000.00M)
                {
                    decimal temppower = decimal.Parse(Power) - 9999.99M;
                    Power = temppower.ToString();
                }
                string sql = "SELECT TOP 1 * FROM Meter_Operation WHERE MO_Operation='E502' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                DataTable tb = DBClass.GetDataTable1(sql);
                if (tb.Rows.Count > 0)
                {
                    string Cell_Id = "0";
                    string Rom_Id = "0";
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                        Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                        Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();
                    sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Remain_Power_Consumption,CPC_Copy_Day) VALUES(";
                    sql += Rom_Id + "," + Cell_Id + "," + Power + ",'" + DateTime.Now.ToString() + "')";
                    DBClass.ExecuteNonQuery(sql);
                }

            }

            if (byteMessage[15] == (byte)(Convert.ToInt16("E5", 16) + 0x33) && byteMessage[14] == (byte)(Convert.ToInt16("02", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";
                string PoNo0 = Convert.ToString(byteMessage[10], 16);
                string PoNo1 = Convert.ToString(byteMessage[9], 16);
                string PoNo2 = Convert.ToString(byteMessage[8], 16);
                string PoNo3 = Convert.ToString(byteMessage[7], 16);
                string PoNo4 = Convert.ToString(byteMessage[6], 16);
                string PoNo5 = Convert.ToString(byteMessage[5], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }
                int ff;
                ff = ((byteMessage[18] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));
                ff = ((byteMessage[17] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[16] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;

                if (decimal.Parse(Power) > 9000.00M)
                {
                    decimal temppower = decimal.Parse(Power) - 9999.99M;
                    Power = temppower.ToString();
                }
                string sql = "SELECT TOP 1 * FROM Meter_Operation WHERE MO_Operation='E502' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                DataTable tb = DBClass.GetDataTable1(sql);
                if (tb.Rows.Count > 0)
                {
                    string Cell_Id = "0";
                    string Rom_Id = "0";
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                        Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                        Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();
                    sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Remain_Power_Consumption,CPC_Copy_Day) VALUES(";
                    sql += Rom_Id + "," + Cell_Id + "," + Power + ",'" + DateTime.Now.ToString() + "')";
                    DBClass.ExecuteNonQuery(sql);
                }

            }

            //水表读数
            if (byteMessage[14] == (byte)(Convert.ToInt16("90", 16) + 0x33) && byteMessage[13] == (byte)(Convert.ToInt16("10", 16) + 0x33))
            {

                //Power.WriteWater("水表读数==byteMessage[14] =");
                string Opera1;
                string PoNo = "";
                string PoNo0 = Convert.ToString(byteMessage[9], 16);
                string PoNo1 = Convert.ToString(byteMessage[8], 16);
                string PoNo2 = Convert.ToString(byteMessage[7], 16);
                string PoNo3 = Convert.ToString(byteMessage[6], 16);
                string PoNo4 = Convert.ToString(byteMessage[5], 16);
                string PoNo5 = Convert.ToString(byteMessage[4], 16);


                if (!string.IsNullOrEmpty(PoNo) && PoNo != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    else if (PoNo0.Length < 1)
                        PoNo0 = "00";
                    // PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    else if (PoNo1.Length < 1)
                        PoNo1 = "00";
                    // PoNo += PoNo1;
                }

                if (PoNo0.Length < 2)
                    PoNo0 = "0" + PoNo0;
                else if (PoNo2.Length < 1)
                    PoNo0 = "00";
                PoNo += PoNo0;

                if (PoNo1.Length < 2)
                    PoNo1 = "0" + PoNo1;
                else if (PoNo2.Length < 1)
                    PoNo1 = "00";
                PoNo += PoNo1;

                if (PoNo2.Length < 2)
                    PoNo2 = "0" + PoNo2;
                else if (PoNo2.Length < 1)
                    PoNo2 = "00";
                PoNo += PoNo2;


                if (PoNo3.Length < 2)
                    PoNo3 = "0" + PoNo3;
                else if (PoNo3.Length < 1)
                    PoNo3 = "00";
                PoNo += PoNo3;


                if (PoNo4.Length < 2)
                    PoNo4 = "0" + PoNo4;
                else if (PoNo4.Length < 1)
                    PoNo4 = "00";
                PoNo += PoNo4;


                if (PoNo5.Length < 2)
                    PoNo5 = "0" + PoNo5;
                else if (PoNo5.Length < 1)
                    PoNo5 = "0" + PoNo5;
                PoNo += PoNo5;

                string sqltemp = "SELECT TOP 1 * FROM Copy_Water WHERE CW_Mater_No='" + PoNo + "'  AND CW_State=1  ORDER BY CW_Id DESC";
                DataTable tbWater = DBClass.GetDataTable(sqltemp);

                //Power.WriteWater("水表：=====" + PoNo);

                if (tbWater.Rows.Count > 0)
                {
                    int ff = 0;
                    ff = ((byteMessage[18] - 0x33));
                    Opera1 = changetwoX(ff.ToString("X"));
                    ff = ((byteMessage[17] - 0x33));
                    Opera1 += changetwoX(ff.ToString("X"));
                    ff = ((byteMessage[16] - 0x33));
                    Opera1 += changetwoX(ff.ToString("X"));
                    ff = ((byteMessage[15] - 0x33));
                    string Point = changetwoX(ff.ToString("X"));
                    string strPoint = Point;


                    string Power1 = Opera1 + "." + strPoint;
                    //MessageBox.Show(Power);
                    ////string sql = "SELECT TOP 1 * FROM Meter_Operation WHERE MO_Operation='9010' AND MO_MB_No='" + PoNo + "' AND MO_State=1 ORDER BY MO_Id DESC";
                    ////DataTable tb = DBClass.GetDataTable(sql);

                    ////string Cell_Id = "0";
                    ////string Rom_Id = "0";
                    ////if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                    ////    Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                    ////if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                    ////    Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();


                    string strCUC_Id = tbWater.Rows[0]["CW_CUC_Id"].ToString();
                    string sql = "SELECT TOP 1 WF_Total FROM Water_Fee WHERE WF_Water_No='" + PoNo + "' AND WF_Create_Date>='2013-09-03' ORDER BY WF_Id DESC";
                    DataTable waterUp = DBClass.GetDataTable(sql);
                    decimal tempWater = 0.0M;
                    if (waterUp.Rows.Count > 0)
                        tempWater = decimal.Parse(Power1) - decimal.Parse(waterUp.Rows[0]["WF_Total"].ToString());
                    else
                        tempWater = 0;
                    sql = "INSERT INTO Water_Fee (WF_Total,WF_Water_No,WF_Type,WF_Create_Date,WF_CUC_Id)VALUES (";
                    sql += Power1 + ",'" + PoNo + "',0,'" + DateTime.Now.ToString() + "'," + strCUC_Id + ")";
                    DBClass.ExecuteNonQuery(sql);
                    sql = "SELECT TOP 1 WF_Id FROM Water_Fee ORDER BY WF_Id DESC";
                    int WF_Id = int.Parse(DBClass.ExecuteScalar(sql));
                    Water water = new Water();
                    water.AddWater_Detail(int.Parse(strCUC_Id), tempWater, WF_Id);

                }






            }

                     //电表读数
            else if (byteMessage[13] == (byte)(Convert.ToInt16("90", 16) + 0x33) && byteMessage[12] == (byte)(Convert.ToInt16("10", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";

                //Power.Write("byteMessage[13]===9010");


                string PoNo0 = Convert.ToString(byteMessage[8], 16);
                string PoNo1 = Convert.ToString(byteMessage[7], 16);
                string PoNo2 = Convert.ToString(byteMessage[6], 16);
                string PoNo3 = Convert.ToString(byteMessage[5], 16);
                string PoNo4 = Convert.ToString(byteMessage[4], 16);
                string PoNo5 = Convert.ToString(byteMessage[3], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }


                int ff = ((byteMessage[16] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));

                ff = ((byteMessage[15] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[14] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;
                string Power1 = Opera1 + "." + strPoint;
                //Power.Write("电表：=====" + Power1);
                if (decimal.Parse(Power1) > 9000.00M)
                {
                    decimal temppower = decimal.Parse(Power1) - 9999.99M;
                    Power1 = temppower.ToString();
                }
                string sql = "SELECT TOP 1 *,ISNULL([MO_Condition],0) as MO_Condition2 FROM Meter_Operation WHERE MO_Operation='9010' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                DataTable tb = DBClass.GetDataTable1(sql);

                string Cell_Id = "0";
                string Rom_Id = "0";
                if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                    Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                    Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();



                sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Active_Power,CPC_Copy_Day) VALUES(";
                sql += Rom_Id + "," + Cell_Id + "," + Power1 + ",'" + DateTime.Now.ToString() + "')";
                DBClass.ExecuteNonQuery(sql);
                Power powerdemo = new Power();

                if (!string.IsNullOrEmpty(Cell_Id) && Cell_Id != "0")
                {
                    if (tb.Rows[0]["MO_Condition2"].ToString() != "-1")
                    {
                        //调用分摊公用电
                        powerdemo.cellPubShare(Cell_Id);
                    }

                }

                if (!string.IsNullOrEmpty(Rom_Id) && Rom_Id != "0")
                {
                    powerdemo.createActivePower(Rom_Id);
                }

            }

                   //电表读数
            else if (byteMessage[17] == (byte)(Convert.ToInt16("90", 16) + 0x33) && byteMessage[16] == (byte)(Convert.ToInt16("10", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";

                //Power.Write("byteMessage[13]===9010");


                string PoNo0 = Convert.ToString(byteMessage[12], 16);
                string PoNo1 = Convert.ToString(byteMessage[11], 16);
                string PoNo2 = Convert.ToString(byteMessage[10], 16);
                string PoNo3 = Convert.ToString(byteMessage[9], 16);
                string PoNo4 = Convert.ToString(byteMessage[8], 16);
                string PoNo5 = Convert.ToString(byteMessage[7], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }
                int ff = ((byteMessage[20] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));

                ff = ((byteMessage[19] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[18] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;
                string Power1 = Opera1 + "." + strPoint;



                //Power.Write("电表：=====" + Power1);
                if (decimal.Parse(Power1) > 9000.00M)
                {
                    decimal temppower = decimal.Parse(Power1) - 9999.99M;
                    Power1 = temppower.ToString();
                }
                string sql = "SELECT TOP 1 *,ISNULL([MO_Condition],0) as MO_Condition2 FROM Meter_Operation WHERE MO_Operation='9010' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                DataTable tb = DBClass.GetDataTable1(sql);

                string Cell_Id = "0";
                string Rom_Id = "0";
                if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                    Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                    Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();



                sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Active_Power,CPC_Copy_Day) VALUES(";
                sql += Rom_Id + "," + Cell_Id + "," + Power1 + ",'" + DateTime.Now.ToString() + "')";
                DBClass.ExecuteNonQuery(sql);
                Power powerdemo = new Power();

                if (!string.IsNullOrEmpty(Cell_Id) && Cell_Id != "0")
                {
                    if (tb.Rows[0]["MO_Condition2"].ToString() != "-1")
                    {
                        //调用分摊公用电
                        powerdemo.cellPubShare(Cell_Id);
                    }

                }

                if (!string.IsNullOrEmpty(Rom_Id) && Rom_Id != "0")
                {
                    powerdemo.createActivePower(Rom_Id);
                }

            }

                  //电表读数
            else if (byteMessage[15] == (byte)(Convert.ToInt16("90", 16) + 0x33) && byteMessage[14] == (byte)(Convert.ToInt16("10", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";
                // Power.Write("byteMessage[15]===9010");




                string PoNo0 = Convert.ToString(byteMessage[10], 16);
                string PoNo1 = Convert.ToString(byteMessage[9], 16);
                string PoNo2 = Convert.ToString(byteMessage[8], 16);
                string PoNo3 = Convert.ToString(byteMessage[7], 16);
                string PoNo4 = Convert.ToString(byteMessage[6], 16);
                string PoNo5 = Convert.ToString(byteMessage[5], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }

                int ff = ((byteMessage[18] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));

                ff = ((byteMessage[17] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[16] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;

                string Power1 = Opera1 + "." + strPoint;


                //Power.Write("电表：=====" + Power1);
                if (decimal.Parse(Power1) > 9000.00M)
                {
                    decimal temppower = decimal.Parse(Power1) - 9999.99M;
                    Power1 = temppower.ToString();
                }
                string sql = "SELECT TOP 1 *,ISNULL([MO_Condition],0) as MO_Condition2 FROM Meter_Operation WHERE MO_Operation='9010' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                DataTable tb = DBClass.GetDataTable1(sql);

                if (tb != null && tb.Rows.Count > 0)
                {
                    string Cell_Id = "0";
                    string Rom_Id = "0";
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                        Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                    if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                        Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();



                    sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Active_Power,CPC_Copy_Day) VALUES(";
                    sql += Rom_Id + "," + Cell_Id + "," + Power1 + ",'" + DateTime.Now.ToString() + "')";
                    // Power.Write(sql);
                    DBClass.ExecuteNonQuery(sql);
                    Power powerdemo = new Power();

                    if (!string.IsNullOrEmpty(Cell_Id) && Cell_Id != "0")
                    {
                        if (tb.Rows[0]["MO_Condition2"].ToString() != "-1")
                        {
                            //调用分摊公用电
                            powerdemo.cellPubShare(Cell_Id);
                        }
                    }

                    if (!string.IsNullOrEmpty(Rom_Id) && Rom_Id != "0")
                    {
                        powerdemo.createActivePower(Rom_Id);
                    }
                }

            }


            else if (byteMessage[13] == (byte)(Convert.ToInt16("E8", 16) + 0x33) && byteMessage[12] == (byte)(Convert.ToInt16("70", 16) + 0x33))
            {
                string Opera1;
                string PoNo = "";
                string PoNo0 = Convert.ToString(byteMessage[8], 16);
                string PoNo1 = Convert.ToString(byteMessage[7], 16);
                string PoNo2 = Convert.ToString(byteMessage[6], 16);
                string PoNo3 = Convert.ToString(byteMessage[5], 16);
                string PoNo4 = Convert.ToString(byteMessage[4], 16);
                string PoNo5 = Convert.ToString(byteMessage[3], 16);

                if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                {
                    if (PoNo0.Length < 2)
                        PoNo0 = "0" + PoNo0;
                    PoNo += PoNo0;
                }
                if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                {
                    if (PoNo1.Length < 2)
                        PoNo1 = "0" + PoNo1;
                    PoNo += PoNo1;
                }
                if (!string.IsNullOrEmpty(PoNo2))
                {
                    if (PoNo2.Length < 2)
                        PoNo2 = "0" + PoNo2;
                    PoNo += PoNo2;
                }
                if (!string.IsNullOrEmpty(PoNo3))
                {
                    if (PoNo3.Length < 2)
                        PoNo3 = "0" + PoNo3;
                    PoNo += PoNo3;
                }
                if (!string.IsNullOrEmpty(PoNo4))
                {
                    if (PoNo4.Length < 2)
                        PoNo4 = "0" + PoNo4;
                    PoNo += PoNo4;
                }
                if (!string.IsNullOrEmpty(PoNo5))
                {
                    if (PoNo5.Length < 2)
                        PoNo5 = "0" + PoNo5;
                    PoNo += PoNo5;
                }
                if (PoNo.Length < 8)
                {
                    string tempNo = "";
                    for (int i = 0; i < 8 - PoNo.Length; i++)
                    {
                        tempNo += "0";
                    }
                    tempNo += PoNo;
                    PoNo = tempNo;

                }


                int ff = ((byteMessage[16] - 0x33));
                Opera1 = changetwoX(ff.ToString("X"));
                ff = ((byteMessage[16] - 0x33));
                Opera1 += changetwoX(ff.ToString("X"));

                ff = ((byteMessage[14] - 0x33));
                string Point = changetwoX(ff.ToString("X"));
                string strPoint = Point;
                string Power = Opera1 + "." + strPoint;



                //string sql = "SELECT TOP 1 * FROM Meter_Operation WHERE MO_Operation='9010' AND MO_MB_No='" + PoNo + "' AND MO_State=1 ORDER BY MO_Id DESC";
                //DataTable tb = DBClass.GetDataTable(sql);

                //string Cell_Id = "0";
                //string Rom_Id = "0";
                //if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Cell_Id"].ToString()))
                //    Cell_Id = tb.Rows[0]["MO_Cell_Id"].ToString();
                //if (!string.IsNullOrEmpty(tb.Rows[0]["MO_Room_Id"].ToString()))
                //    Rom_Id = tb.Rows[0]["MO_Room_Id"].ToString();



                //sql = "INSERT INTO Take_Power_Consumption (CPC_Room_Id,CPC_Cell_Id,CPC_Active_Power,CPC_Copy_Day) VALUES(";
                //sql += Rom_Id + "," + Cell_Id + "," + Power + ",'" + DateTime.Now.ToString() + "')";
                //DBClass.ExecuteNonQuery(sql);


            }

            else
            {
                int begin = 0;
                int end = 0;
                for (int i = 0; i < byteMessage.Length; i++)
                {
                    if ((int)byteMessage[i] == 254 && begin == 0)
                    {
                        begin = i;
                    }
                    else if ((int)byteMessage[i] == 22 && end == 0)
                    {
                        end = i;
                    }


                }

                if (end > begin)
                {
                    byte[] bytetemp = new byte[end - begin + 1];
                    int point = 0;
                    for (int j = begin; j < end; j++)
                    {
                        bytetemp[point] = byteMessage[j];
                        point++;
                    }
                    Power po = new Power();
                    string[] ArrHexstr = po.Get16Byte(bytetemp);
                    string Hexstr = "";
                    for (int m = 0; m < ArrHexstr.Length; m++)
                    {
                        Hexstr += ArrHexstr[m];
                    }
                    int Opera1;
                    string PoNo = "";
                    string PoNo0 = Convert.ToString(byteMessage[8], 16);
                    string PoNo1 = Convert.ToString(byteMessage[7], 16);
                    string PoNo2 = Convert.ToString(byteMessage[6], 16);
                    string PoNo3 = Convert.ToString(byteMessage[5], 16);
                    string PoNo4 = Convert.ToString(byteMessage[4], 16);
                    string PoNo5 = Convert.ToString(byteMessage[3], 16);

                    if (!string.IsNullOrEmpty(PoNo0) && PoNo0 != "0")
                    {
                        if (PoNo0.Length < 2)
                            PoNo0 = "0" + PoNo0;
                        PoNo += PoNo0;
                    }
                    if (!string.IsNullOrEmpty(PoNo1) && PoNo1 != "0")
                    {
                        if (PoNo1.Length < 2)
                            PoNo1 = "0" + PoNo1;
                        PoNo += PoNo1;
                    }
                    if (!string.IsNullOrEmpty(PoNo2))
                    {
                        if (PoNo2.Length < 2)
                            PoNo2 = "0" + PoNo2;
                        PoNo += PoNo2;
                    }
                    if (!string.IsNullOrEmpty(PoNo3))
                    {
                        if (PoNo3.Length < 2)
                            PoNo3 = "0" + PoNo3;
                        PoNo += PoNo3;
                    }
                    if (!string.IsNullOrEmpty(PoNo4))
                    {
                        if (PoNo4.Length < 2)
                            PoNo4 = "0" + PoNo4;
                        PoNo += PoNo4;
                    }
                    if (!string.IsNullOrEmpty(PoNo5))
                    {
                        if (PoNo5.Length < 2)
                            PoNo5 = "0" + PoNo5;
                        PoNo += PoNo5;
                    }
                    if (PoNo.Length < 8)
                    {
                        string tempNo = "";
                        for (int i = 0; i < 8 - PoNo.Length; i++)
                        {
                            tempNo += "0";
                        }
                        tempNo += PoNo;
                        PoNo = tempNo;

                    }
                    Opera1 = (((byteMessage[16] - 0x33) / 16) * 10 + ((byteMessage[16] - 0x33) % 16)) * 100;
                    Opera1 += ((byteMessage[15] - 0x33) / 16) * 10 + ((byteMessage[15] - 0x33) % 16);


                    string sql = "SELECT TOP 1 * FROM Meter_Operation_Debug WHERE MO_Operation='" + Opera1 + "' AND MO_MB_No='" + PoNo + "' ORDER BY MO_Id DESC";
                    DataTable tb = DBClass.GetDataTable(sql);

                    string Cell_Id = "0";


                    sql = "UPDATE  Meter_Operation_Debug (MO_Recive_Report) VALUES('";
                    sql += Hexstr + "')";
                    DBClass.ExecuteNonQuery(sql);



                }

            }
        }

        private byte[] Getbyte(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            return bytes;
        }

        /// <summary>
        /// 无条件断电(C03C)
        /// </summary>
        /// <param name="operton">操作符</param>
        /// <param name="PowerNo">电表编号</param>
        /// <param name="pwd">密码</param>
        /// <returns>byte 流</returns>
        public byte[] CartPower(string operton, string PowerNo, string pwd)
        {
            PowerCard.Power po = new Power();
            byte[] byteall = new byte[19];
            byte[] mo = new byte[17];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;
            //0  1   234567 8  9  10 11 12 13141516 17 18
            //FE 68 powerno 68 04 06 opera password MO 16


            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x04);
            mo[9] = (byte)(0x04);
            byteall[10] = (byte)(0x06);
            mo[10] = (byte)(0x06);
            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }

            byte[] pwdbyte = po.pwdbyte(pwd);

            for (int i = 0; i < pwdbyte.Length; i++)
            {
                byteall[13 + i] = pwdbyte[i];
                mo[13 + i] = pwdbyte[i];
            }

            byteall[17] = po.getmol(mo);

            byteall[18] = (byte)(0x16);

            return byteall;
        }

        /// <summary>
        /// 无条件通电(C03D)
        /// </summary>
        /// <param name="operton">操作符</param>
        /// <param name="PowerNo">电表编号</param>
        /// <param name="pwd">密码</param>
        /// <returns>byte 流</returns>
        public byte[] UpPower(string operton, string PowerNo, string pwd)
        {
            PowerCard.Power po = new Power();
            byte[] byteall = new byte[19];
            byte[] mo = new byte[17];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;



            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x04);
            mo[9] = (byte)(0x04);
            byteall[10] = (byte)(0x06);
            mo[10] = (byte)(0x06);
            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }

            byte[] pwdbyte = po.pwdbyte(pwd);

            for (int i = 0; i < pwdbyte.Length; i++)
            {
                byteall[13 + i] = pwdbyte[i];
                mo[13 + i] = pwdbyte[i];
            }

            byteall[17] = po.getmol(mo);

            byteall[18] = (byte)(0x16);

            return byteall;
        }

        /// <summary>
        /// 通电控制，功率限额用电，无预付费通预付费
        /// </summary>
        /// <param name="operton">操作</param>
        /// <param name="PowerNo">电表号</param>
        /// <param name="pwd">密码</param>
        /// <returns>byte</returns>
        public byte[] noyufu(string operton, string PowerNo, string pwd)
        {

            PowerCard.Power po = new Power();
            byte[] byteall = new byte[19];
            byte[] mo = new byte[17];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;



            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x04);
            mo[9] = (byte)(0x04);
            byteall[10] = (byte)(0x06);
            mo[10] = (byte)(0x06);
            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }


            //byteall[13]=(byte)(Convert.ToInt16("2", 16) + 0x33);
            //mo[13] = (byte)(Convert.ToInt16("2", 16) + 0x33);

            byte[] pwdbyte = po.pwdbyte(pwd);

            for (int i = 0; i < pwdbyte.Length; i++)
            {
                byteall[13 + i] = pwdbyte[i];
                mo[13 + i] = pwdbyte[i];
            }


            //for (int i = 0; i < opera.Length; i++)
            //{
            //    byteall[17 + i] = opera[i];
            //    mo[17 + i] = opera[i];
            //}
            //for (int i = 0; i < 4; i++)
            //{
            //    byteall[19 + i] = (byte)0x33;
            //    mo[19 + i] = (byte)0x33;
            //}

            //byteall[17 + i] = opera[i];
            //mo[17 + i] = opera[i];




            //for (int i = 0; i < kw1.Length; i++)
            //{
            //    byteall[17 + i] = kw1[i];
            //    mo[17 + i] = kw1[i];
            //}


            byteall[17] = po.getmol(mo);

            byteall[18] = (byte)(0x16);

            return byteall;


        }

        /// <summary>
        /// 最大用电负荷
        /// </summary>
        /// <param name="operton">操作</param>
        /// <param name="PowerNo">电表号</param>
        /// <param name="pwd">密码</param>
        /// <param name="fuhe">用电负荷</param>
        /// <returns>byte</returns>
        public byte[] powerfuhe(string operton, string PowerNo, string pwd, string fuhe)
        {

            PowerCard.Power po = new Power();
            byte[] byteall = new byte[21];
            byte[] mo = new byte[19];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;



            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x04);
            mo[9] = (byte)(0x04);
            byteall[10] = (byte)(0x08);
            mo[10] = (byte)(0x08);
            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }


            //byteall[13]=(byte)(Convert.ToInt16("2", 16) + 0x33);
            //mo[13] = (byte)(Convert.ToInt16("2", 16) + 0x33);

            byte[] pwdbyte = po.pwdbyte(pwd);

            for (int i = 0; i < pwdbyte.Length; i++)
            {
                byteall[13 + i] = pwdbyte[i];
                mo[13 + i] = pwdbyte[i];
            }




            byte[] kw1 = po.getkw(fuhe);

            for (int i = 0; i < kw1.Length; i++)
            {
                byteall[17 + i] = kw1[i];
                mo[17 + i] = kw1[i];
            }


            byteall[19] = po.getmol(mo);

            byteall[20] = (byte)(0x16);

            return byteall;
        }

        /// <summary>
        /// 购电
        /// </summary>
        /// <param name="operton">操作</param>
        /// <param name="PowerNo">表号</param>
        /// <param name="pwd">密码</param>
        /// <param name="Power">电量</param>
        /// <param name="PowerCount">充电次数</param>
        /// <returns>byte</returns>
        public byte[] powerAdd(string operton, string PowerNo, string pwd, string Power, int PowerCount)
        {
            PowerCard.Power po = new Power();
            byte[] byteall = new byte[24];
            byte[] mo = new byte[22];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;



            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x04);
            mo[9] = (byte)(0x04);
            byteall[10] = (byte)(0x0B);
            mo[10] = (byte)(0x0B);



            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }

            byte[] pwdbyte = po.pwdbyte(pwd);

            for (int i = 0; i < pwdbyte.Length; i++)
            {
                byteall[13 + i] = pwdbyte[i];
                mo[13 + i] = pwdbyte[i];
            }


            byte[] powerbyte = po.PayPower(decimal.Parse(Power), PowerCount);
            for (int i = 0; i < powerbyte.Length; i++)
            {
                byteall[17 + i] = powerbyte[i];
                mo[17 + i] = powerbyte[i];
            }

            byteall[22] = po.getmol(mo);

            byteall[23] = (byte)(0x16);

            return byteall;
        }

        /// <summary>
        /// 抄剩余电量 和有功电量
        /// </summary>
        /// <param name="operton">操作数</param>
        /// <param name="PowerNo">电表编号</param>
        /// <param name="pwd">密码</param>
        /// <returns>byte</returns>
        public byte[] CopyRemainPower(string operton, string PowerNo, string pwd)
        {
            PowerCard.Power po = new Power();
            byte[] byteall = new byte[15];
            byte[] mo = new byte[13];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;



            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x01);
            mo[9] = (byte)(0x01);
            byteall[10] = (byte)(0x02);
            mo[10] = (byte)(0x02);
            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }


            //byteall[13]=(byte)(Convert.ToInt16("2", 16) + 0x33);
            //mo[13] = (byte)(Convert.ToInt16("2", 16) + 0x33);







            byteall[13] = po.getmol(mo);

            byteall[14] = (byte)(0x16);

            return byteall;
        }

        public string changetwoX(string hex)
        {
            if (hex.Length < 2)
                return "0" + hex;
            else
                return hex;
        }

        /// <summary>
        /// 设定允许超负荷断电次数E874，A0无限次,恶性负载断电次数E876，A0无限次,警起始电量E503，30kwh
        /// </summary>
        /// <param name="operton">操作</param>
        /// <param name="PowerNo">电表号</param>
        /// <param name="pwd">密码</param>
        /// <param name="fuhe">用电负荷</param>
        /// <returns>byte</returns>
        public byte[] powerqita(string operton, string PowerNo, string pwd, string fuhe)
        {

            PowerCard.Power po = new Power();
            byte[] byteall = new byte[20];
            byte[] mo = new byte[18];
            byteall[0] = (byte)0xFE;
            mo[0] = (byte)0x00;
            byteall[1] = (byte)0x68;
            mo[1] = (byte)0x68;



            byte[] byteAddress = po.PowerNobyte(PowerNo);
            for (int i = 0; i < byteAddress.Length; i++)
            {
                byteall[i + 2] = byteAddress[i];
                mo[i + 2] = byteAddress[i];
            }
            byteall[8] = (byte)(0x68);
            mo[8] = (byte)(0x68);
            byteall[9] = (byte)(0x04);
            mo[9] = (byte)(0x04);
            byteall[10] = (byte)(0x07);
            mo[10] = (byte)(0x07);
            byte[] opera = po.getOpera(operton);

            for (int i = 0; i < opera.Length; i++)
            {
                byteall[11 + i] = opera[i];
                mo[11 + i] = opera[i];
            }


            //byteall[13]=(byte)(Convert.ToInt16("2", 16) + 0x33);
            //mo[13] = (byte)(Convert.ToInt16("2", 16) + 0x33);

            byte[] pwdbyte = po.pwdbyte(pwd);

            for (int i = 0; i < pwdbyte.Length; i++)
            {
                byteall[13 + i] = pwdbyte[i];
                mo[13 + i] = pwdbyte[i];
            }




            byte kw1 = (byte)(Convert.ToInt16(fuhe, 16) + 0x33);

            //for (int i = 0; i < kw1.Length; i++)
            //{
            byteall[17] = kw1;
            mo[17] = kw1;
            //}


            byteall[18] = po.getmol(mo);

            byteall[19] = (byte)(0x16);

            return byteall;
        }

    }
}
